package com.youkeda.kafka.study.service.impl;

import com.youkeda.kafka.study.model.SummaryData;
import com.youkeda.kafka.study.service.StudyDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName StudyDataServiceImpl
 * @Author 刘正星
 * @Date 2021/4/29 上午10:15
 * @Description
 */
@Service
public class StudyDataServiceImpl implements StudyDataService {

    // 采用线程安全的本地存储，模拟数据库

    public static final Map<String,SummaryData> SUMMARY_DATA_MAP = new ConcurrentHashMap<>();

    @Override
    public void saveStudyData(SummaryData data) {
        if (data == null || StringUtils.isBlank(data.getName())) {
            return;
        }
        SUMMARY_DATA_MAP.put(data.getName(),data);
    }

    @Override
    public SummaryData findStudyData(String name) {
        if (StringUtils.isBlank(name)){
            SummaryData summaryData = new SummaryData();
            summaryData.setName(name);
            return summaryData;
        }
        return SUMMARY_DATA_MAP.get(name);
    }
}
